package com.zbl.zblproject.hrm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.hrm.entity.HrmDepartment;
import com.zbl.zblproject.hrm.entity.HrmEmployee;
import com.zbl.zblproject.hrm.mapper.HrmDepartmentMapper;
import com.zbl.zblproject.hrm.mapper.HrmEmployeeMapper;
import com.zbl.zblproject.hrm.service.HrmEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/29
 */
@Service
@Transactional
public class HrmEmployeeServiceImpl implements HrmEmployeeService {

    @Autowired
    private HrmEmployeeMapper hrmEmployeeMapper;

    @Autowired
    private HrmDepartmentMapper hrmDepartmentMapper;


    @Override
    public boolean addEmployee(HrmEmployee employee) {
        return hrmEmployeeMapper.insert(employee) == 1;
    }

    @Override
    public boolean deleteEmployeById(int id) {
        return hrmEmployeeMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateEmployeeById(HrmEmployee employee) {
        return hrmEmployeeMapper.updateById(employee) == 1;
    }

    @Override
    public IPage<HrmEmployee> pageEmployee(Page page, int status, String search) {
        QueryWrapper<HrmEmployee> wrapper = new QueryWrapper<>();
        if(status != -1){
            wrapper.eq("status", status);
        }
        if(!search.trim().equals("")){
            wrapper.like("name", search);
        }
        IPage<HrmEmployee> employeeIPage = hrmEmployeeMapper.selectPage(page, wrapper);
        employeeIPage.getRecords().forEach(x->{
            x.setDepartment(hrmDepartmentMapper.selectById(x.getDepartmentId()));
        });
        return employeeIPage;
    }

    @Override
    public IPage<HrmDepartment> pageDepartment(Page page, String search) {
        QueryWrapper<HrmDepartment> wrapper = new QueryWrapper<>();
        if(!search.trim().equals("")){
            wrapper.like("department_name", search);
        }
        IPage<HrmDepartment> departmentIPage = hrmDepartmentMapper.selectPage(page, wrapper);
        return departmentIPage;
    }


    @Override
    public boolean addDepartment(HrmDepartment department) {
        return hrmDepartmentMapper.insert(department) == 1;
    }

    @Override
    public boolean deleteDepartmentById(int id) {
        return hrmDepartmentMapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateDepartmentById(HrmDepartment department) {
        return hrmDepartmentMapper.updateById(department) == 1;
    }

    @Override
    public List<HrmDepartment> allDepartment() {
        List<HrmDepartment> departments = getDepartmentsByParent(0);
        return departments;
    }

    @Override
    public List<HrmDepartment> allDepartmentList() {
        List<HrmDepartment> departments = hrmDepartmentMapper.selectList(null);
        return departments;
    }

    private List<HrmDepartment> getDepartmentsByParent(int parent){
        List<HrmDepartment> departments;
        QueryWrapper<HrmDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("parent", parent);
        departments = hrmDepartmentMapper.selectList(wrapper);
        departments.forEach(x ->{
          x.setChildren(getDepartmentsByParent(x.getId()));
        });
        return departments;
    }
}
